//
//  CDSettingThirdCell.m
//  CuriosityDaily3
//
//  Created by donghao on 16/9/24.
//  Copyright © 2016年 theBestTeam. All rights reserved.
//

#import "CDSettingThirdCell.h"
#import "CDSettingPF.h"
#import "UIImage+CDFillImage.h"


@interface CDSettingThirdCell ()<CDSetSwitchViewDelegate>

@property(nonatomic,weak) UIButton *leftButton;
@property(nonatomic,weak) CDSetSwitchView *switchView;
// 两种状态的图片
@property(nonatomic,strong) UIImage *image;
@property(nonatomic,strong) UIImage *selImage;

@property(nonatomic,weak) UIView *colorView;

@end

@implementation CDSettingThirdCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

/**
 *  设置界面
 */
- (void)setupUI {
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:leftButton];
    
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(13);
    }];
    
    UIView *colorView = [UIView new];
    self.colorView.frame = leftButton.frame;
    [self.contentView addSubview:colorView];
    colorView.alpha = 0.3;
    self.colorView = colorView;
    
    
    leftButton.userInteractionEnabled = NO;
    self.leftButton = leftButton;
    
    CDSetSwitchView *switchView = [[CDSetSwitchView alloc] init];
    switchView.delegate = self;
    [self.contentView addSubview:switchView];
    [switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-20);
        make.size.mas_equalTo(CGSizeMake(36, 20));
    }];
    self.switchView = switchView;
    
    
    
}
#pragma mark - 开关视图的代理
- (void)switchView:(CDSetSwitchView *)switchView stateChanged:(CDSwitchState)switchState {
    if (switchState == CDSwitchStateNormal) {
        // 灰色
//        [self.leftButton setImage:[self.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//        [self.leftButton setImage:[self.selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
        
        self.colorView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.464];
        
    } else {
        // 亮色
//        [self.leftButton setImage:[self.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//        [self.leftButton setImage:[self.selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
        self.colorView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.000];
    }
    [self layoutIfNeeded];
}


- (void)setSwitchViewState:(CDSwitchState)switchState {
    self.switchView.switchState = switchState;
    
    [self layoutIfNeeded];
}


- (void)setLeftButtonWithImage:(UIImage *)image selectedImaeg:(UIImage *)selectImage {
    
    self.image = [image fitToSize:CGSizeMake(30, 30)];
    self.selImage = [selectImage fitToSize:CGSizeMake(30, 30)];
    [self.leftButton setImage:self.image  forState:UIControlStateNormal];
    [self.leftButton setImage:self.selImage forState:UIControlStateSelected];
}

- (void)setLeftButtonWithImageName:(NSString *)imageName selectedImageName:(NSString *)selectImg  {
    
    if (imageName || selectImg) {
        [self.leftButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [self.leftButton setImage:[UIImage imageNamed:selectImg] forState:UIControlStateSelected];
    }
    
}

- (void)setLeftButtonWIthTitle:(NSString *)title {
    [self.leftButton setTitle:title forState:UIControlStateNormal];
    self.leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
}

//- (void)changeLeftButtonState {
//    if (self.leftButton.state == normal) {
//        self.leftButton.selected = YES;
//    } else {
//        self.leftButton.selected = NO;
//    }
//}
@end
